package Algorithm.SearchAlgorithmDome;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * ClassName:BasicSearch003
 * Package:Algorithm.SearchAlgorithmDome
 * Description:
 *
 * @Author:ZhangXin
 * @Create2023/2/3-18:13
 * @Version:v1.0
 */
public class BasicSearch003 {
    /*需求：定义一个方法利用基本查找,查询某个元素在数组中的索引
    * 要求：需要考虑数组中的元素重复
    * {131,127,147,81,103,23,7,79}*/
    public static void main(String[] args) {

        int[] arr = {131,127,147,81,103,23,7,79,81,48,116,8,81};
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你想要查找的数据：");
        int number = sc.nextInt();
        ArrayList<Integer> list =basicSearch(arr,number);
        for (int i = 0; i < list.size(); i++) {
            System.out.println("所查找的数据在数组的"+list.get(i)+"索引上");
        }
    }

    //返回多个数据的话,可以吧这些数据放到数组或者集合中
    public static ArrayList<Integer> basicSearch(int []arr, int number){
        ArrayList<Integer> list =new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            if (arr[i]==number){
                list.add(i);
            }
        }
        return list;
    }
}
